<script type="text/javascript">
var shown = 0;//al inicializar no se muestra
var shown_last = null;
function onScroll(){
    var pixels_vertically_scrolled = $(window).scrollTop();
    var document_height = $(document).height();
    var calc = Math.floor(document_height/3);

    if(pixels_vertically_scrolled>calc){
        shown_last = 1;
    }else{
        shown_last = 0;
    }

    if(shown!=shown_last){
        if(shown_last==1){
            $("#fixed").animate({"right": "+=350px"}, "slow");
            shown = 1;
        }else{
            $("#fixed").animate({"right": "-=350px"}, "slow");
            shown = 0;
        }
    }
}

function shareOnFb(uri){
    FB.ui(
        {
          method: 'feed',
          name: 'Opinics',
          link: 'http://opinics.practil.com',
          picture: 'https://fbcdn-photos-a.akamaihd.net/photos-ak-snc1/v27562/89/137726579622329/app_1_137726579622329_8447.gif',
          caption: 'Opinics Caption',
          description: 'Opinics Description',
          message: 'Opinics Message'
        }
    );
}
/***************************** ARTICLES/STEP1 *****************************/
function setCosto(value){
    if(value=="1"){
        $("#divCostPub").show();
        $("#divCostSpec").hide();
    }else if(value=="2"){
        $("#divCostPub").hide();
        $("#divCostSpec").show();
    }else{
        alert("unrecognized type");
    }
}
function loadSubcategories1(cat_id, recall){
    $("#chksubcategoria").removeAttr('checked');
    $("#cboSubcategoria").removeAttr("disabled");
    $("#aux_subcategoria").hide();
    /* seteamos el value del select a un hidden */
    $("#hdCboCategoria").val(cat_id);
    $.ajax({
        type: "post",
        url: "<?php echo url_for('@ajaxLoadSubcategoriesFrm') ?>",
        data: "category_id="+cat_id,
        success: function(html){
            $("#divAjaxLoadSubcategoriesFrm").html(html);
            recall();
        }
    });
}
function selectSubcategory(subcat_id){
    /* seteamos el value del select a un hidden */
    $("#hdCboSubcategoria").val(subcat_id);
}
function sugerirCategoria(){
    if($("#chkcategoria").is(':checked')){
        /* $("#cboCategoria").val('1'); */
        $("#cboCategoria").attr("disabled","disabled");
        $("#cboSubcategoria").attr("disabled","disabled");
        /* loadSubcategories1('1', function(){
            $("#cboSubcategoria").attr("disabled","disabled");
            $("#cboSubcategoria").val('1');
        }); */
        $("#aux_categoria").show();
        $("#chksubcategoria").attr("checked",true);
        sugerirSubcategoria();
    }else{
        $("#cboCategoria").removeAttr("disabled");
        $("#cboSubcategoria").removeAttr("disabled");
        $("#chksubcategoria").removeAttr("checked");
        $("#aux_categoria").hide();
        $("#aux_subcategoria").hide();
    }
}
function sugerirSubcategoria(){
    if($("#chksubcategoria").is(':checked')){
        $("#cboSubcategoria").attr("disabled","disabled");
        /* loadSubcategories1('1', function(){
            $("#cboSubcategoria").attr("disabled","disabled");
            $("#cboSubcategoria").val('1');
        }); */
        $("#aux_subcategoria").show();
        $("#aux_subcategoria").children().focus();
        $("#chksubcategoria").attr("checked",true);
    }else{
        if(!$("#chkcategoria").is(':checked')){
            $("#cboSubcategoria").removeAttr("disabled");
        }
        $("#aux_subcategoria").hide();
    }
}
function v_frm(_self){
    tinymce.triggerSave();/* fuerza el tinyMCE */
    var err = 0;
    var title = $("#title").val();
    var type = $("#type").val();
    var category = $("#hdCboCategoria").val();
    var aux_category = $("#aux_category").val();
    var subcategory = $("#hdCboSubcategoria").val();
    var aux_subcategory = $("#aux_subcategory").val();
    var txt_summary = $("#txt_summary").val();
    var txt_content = $("#txt_content").val();
    /* validaciones */
    if(title==''){err++;alert('Title required');return false;}
    if(type==''){err++;alert('Type required');return false;}
    if(category=='' && aux_category==''){err++;alert('Category required');return false;}
    if(subcategory=='' && aux_subcategory==''){err++;alert('Subcategory required');return false;}
    if(txt_summary==''){err++;alert('Summary required');return false;}
    if(txt_content==''){err++;alert('Content required');return false;}
    if(err>0){ return false; }
    $(_self).attr('disabled',true);
    $('#frm').submit();
}
function autoSave(){
    tinymce.triggerSave();/* fuerza el tinyMCE */
    var id = $("#hdArticleId").val();
    var title = $("#title").val();
    var country = $("#country").val();
    var language = $("#language").val();
    var type = $("#type").val();
    var costPub = $("#costPub").val();
    var costSpec = $("#costSpec").val();
    var category = $("#cboCategoria").val();
    var subcategory = $("#cboSubcategoria").val();
    var txt_summary = $("#txt_summary").val();
    var txt_content = $("#txt_content").val();
    var hd_is_response = $("#hdIsResponseOf").val();
    if(title!='' || txt_summary!='' || txt_content!=''){
        $.ajax({
            type: "post",
            url: "<?php echo url_for('@ajaxArticleAutoSave') ?>",
            data: "id="+id+"&title="+title+"&country="+country+"&language="+language+
                  "&type="+type+"&costPub="+costPub+"&costSpec="+costSpec+
                  "&category="+category+"&subcategory="+subcategory+
                  "&summary="+txt_summary+"&content="+txt_content+"&hd_is_response="+hd_is_response,
            success: function(data){
                /* cuando se hace el autoSave se sobreescribe el valor VALUE del HIDDEN hdIsDraft
                 * para eliminar el borrador en caso se grabe el registro finalizado */
                //$("#hdIsDraft").val(data);/* data devuelve el ID del borrador grabado con estos datos */
                $('#btnAutoSave').attr('disabled',true);
                $("#btnDiscard").attr('disabled',false);
            }
        });
        setTimeout("$('#btnAutoSave').attr('disabled',false)",1000);
    }
    return false;
}
/***************************** ARTICLES/STEP2 ****************************/
function v_frm2(_self){
    printParagraphs();
    $(_self).attr('disabled',true);
    $('#frm').submit();
}
function printParagraphs(){
    var paragraphs = [];
    $("#paragraph_container ul li").each(function() {
        paragraphs.push($(this).attr('id'));
    });
    $("#hdparagraphs").val(paragraphs);
};
/*********************** INCLUDES/_DASHBOARD_INTERESTS **********************/
function hideInterests(){
    $.ajax({
        type: "post",
        url: "<?php echo url_for('@ajaxHideInterests') ?>",
        complete: function(html){
            $('#adviseInterests').hide();
        }
    });
}
/*********************** INCLUDES/_SUBMENU **********************/
function loadSubcategories2(cat_id,callback){
    $("#loading").show();
    $.ajax({
        type: "post",
        url: "<?php echo url_for('@ajaxLoadSubcategories') ?>",
        data: "category_id="+cat_id,
        success: function(html){
            if(html==''){
                document.location.href="<?php echo url_for('@homepage?ca=') ?>"+cat_id;
            }else{
                $("#divAjaxLoadSubcategories").hide().html(html).show();
            }
        },
        complete: function(){
            $("#loading").hide();
            callback();
        }
    });
}
</script>